package com.igoal.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;

@Entity
@Table(name = "comment")
public class Comment extends BaseObject implements Audit, ItemOwner {

	private static final long serialVersionUID = -244742362169050031L;
	
	private Long id;
	private String title;
	private String guestName;
	private String guestEmail;
	private Long componentId;
	private ComponentEnum component;
	private String body;
	private Date createDate;
	private Date updateDate;
	private DataStatus status;

	private User user;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@SearchableId
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	@Column(name="title", nullable=true)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(name="guest_name", nullable=true)
	public String getGuestName() {
		return guestName;
	}

	public void setGuestName(String guestName) {
		this.guestName = guestName;
	}
	
	@Column(name="guest_email", nullable=true)
	public String getGuestEmail() {
		return guestEmail;
	}

	public void setGuestEmail(String guestEmail) {
		this.guestEmail = guestEmail;
	}
	
	@Column(name="component_id", nullable=false)
	public Long getComponentId() {
		return componentId;
	}

	public void setComponentId(Long componentId) {
		this.componentId = componentId;
	}
	
	@Enumerated(EnumType.STRING)
	@Column(name="component", nullable=false)
	public ComponentEnum getComponent() {
		return component;
	}

	public void setComponent(ComponentEnum component) {
		this.component = component;
	}
	
	@Column(name="body", nullable=false)
	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}
	
	@ManyToOne
	@JoinColumn(name="user_id", nullable=true)
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="cdate")
	public Date getCreateDate() {		
		return this.createDate;
	}

	public void setCreateDate(Date date) {
		this.createDate = date;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="mdate")
	public Date getUpdateDate() {
		return this.updateDate;
	}

	public void setUpdateDate(Date date) {
		this.updateDate = date;
	}
	
	@Enumerated(EnumType.STRING)
	@Column(nullable=false, name="status")
	@SearchableProperty
	public DataStatus getStatus() {
		return this.status;
	}

	public void setStatus(DataStatus dataStatus) {
		this.status = dataStatus;
	}

	@Override
	public String toString() {
		String username = this.guestName;
		String email = this.guestEmail;
		
		if(user != null){
			username = user.getUsername();
			email = user.getEmail();
		}
		
		StringBuffer str = new StringBuffer();
		str.append(username).append("[").append(email).append("]");
		str.append(" comments on ").append(" component ").append(component).append("[").append(componentId).append("]");
		str.append(" on ").append(createDate);
		
		return str.toString();
	}

	@Override
	public boolean equals(Object o) {
		if(o instanceof Comment){
			Comment that = (Comment) o;
			return this.id.equals(that.id) && this.component.equals(that.component) && this.componentId.equals(that.componentId);
		}
		
		return false;
	}

	@Override
	public int hashCode() {
		return this.id.hashCode();
	}
	
	@Transient
	public User getOwner() {
		return user;
	}

}
